/***************************
* 1.首页 index
* 2.登陆、注册 初始化
* 3.微信数据 初始化
****************************/

//首页初始化
myApp.onPageInit('index', function (page) {

    // if(iss_localStorage.get("yuqing_firstshow")!="" && iss_localStorage.get("yuqing_firstshow") != ISS.utils.dateToStr("dd_yuqing",new Date()))
    {
        iss_localStorage.set("yuqing_firstshow", "");
    }

    //最新活动
    // var conditionParams_activity = JSON.stringify({
    //     type: 0,
    //     Identifier: ISS_Constants.Identifier.activity,
    //     pageIndex: 1,
    //     pageSize: 1,
    // });
    // $$("#index-page .new-activity .activity-content").text("暂无活动");
    // var activity = iss_localStorage.get("yuqing_activity");
    // if (activity != "") {
    //     var obj = JSON.parse(activity);
    //     $$("#index-page .new-activity .activity-content").text(obj.Head);
    //     $$("#index-page .new-activity .activity-content").attr("data-href", "../../view-yuqing/activity/detail.html?sid=" + obj.ContentID + "&scode=" + obj.ContentCode);
    // }
    // $$.ajax({
    //     // url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.CMS,
    //     url: ISS_Constants.Url.const_activity_url + "?format=json",                    
    //     crossDomain: true,
    //     // data: { content: conditionParams_activity },
    //     data: { Identifier: ISS_Constants.Identifier.activity,
    //         pageIndex: 1,
    //         pageSize: 1 },
    //     dataType: "jsonp",
    //     method: "GET",
    //     success: function (data, status) {
    //         data = JSON.parse(data);
    //         if (data && data.status == "0" && data.results.length > 0) {
    //             iss_localStorage.set("yuqing_activity", JSON.stringify(data.results[0]));
    //             $$("#index-page .new-activity .activity-content").text(data.results[0].Head);
    //             $$("#index-page .new-activity .activity-content").attr("data-href", "../../view-yuqing/activity/detail.html?sid=" + data.results[0].ContentID + "&scode=" + data.results[0].ContentCode);
    //         }
    //     }
    // });

    //最新活动
    var conditionParams_activity = JSON.stringify({
        type: 0,
        Identifier: "dynamic/travelassociation",
        pageIndex: 1,
        pageSize: 1,
    });
    $$("#index-page .new-activity .activity-content").text("暂无活动");
    var activity = iss_localStorage.get("yuqing_activity");
    if (activity != "") {
        var obj = JSON.parse(activity);
        $$("#index-page .new-activity .activity-content").text(obj.Head);
        $$("#index-page .new-activity .activity-content").attr("data-href", "../../view-yuqing/news/newsDetail.html?scode=" + obj.ContentCode);
    }
    $$.ajax({
        // url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.CMS,
        url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.CMS,               
        crossDomain: true,
        data: { content: conditionParams_activity },
        dataType: "jsonp",
        method: "POST",
        success: function (data, status) {
            data = JSON.parse(data);
            if (data && data.status == "0" && data.results.length > 0) {
                iss_localStorage.set("yuqing_activity", JSON.stringify(data.results[0]));
                $$("#index-page .new-activity .activity-content").text(data.results[0].Head);
                $$("#index-page .new-activity .activity-content").attr("data-href", "../../view-yuqing/news/newsDetail.html?scode=" + data.results[0].ContentCode);
            }
        }
    });

    if (iss_localStorage.get("yuqing_firstshow") == "") {
        iss_localStorage.set("yuqing_firstshow", ISS.utils.dateToStr("dd_yuqing", new Date()));
        // initTips();
    }

    function initTips() {
        //如果第一次加载则显示下载APP的Notification
        if (iss_localStorage.get("yuqing_index_tip") == 0) {
            myApp.addNotification({
                // hold: 5000,
                additionalClass: "appNotice",
                media: '<img width="30" height="30" src="../../assets/images/images-yuqing/index/Icon.png">',
                title: '推荐下载野三坡旅游APP ',
                closeIcon: false,
            });
            iss_localStorage.set("yuqing_index_tip", 1);
            //js添加按钮
            $$(".item-title-row").append('<div class="item-title"><div><img class="btndown" src="../../assets/images/images-yuqing/index/download.png" width="60" height="30" style="float:left;margin-top:3px;"/><img class="btnclose" src="../../assets/images/images-yuqing/index/close.png" width="60" height="30" style="float:right;margin-left:10px;margin-top:3px;"/></div></div>');
            //下载按钮
            $$(".notifications .btndown").on("click", function () {
                window.location.href = "http://119.10.53.45/Common/App/Mobile.html";
                //window.open("http://119.10.53.45/Common/App/Mobile.html");
            });

            //关闭按钮
            $$(".notifications .btnclose").on("click", function () {
                myApp.closeNotification(".appNotice");
            });

        } //end if

    };

    //获取天气
    $$.ajax({
        url: "http://222.86.120.149:30087/tdp.api",
        data: {
            methodName: "GetWeather"
        },
        crossDomain:true,
        dataType: "json",
        method: "POST",
        success: function (data, status) {
            if (data && data != null && data.ResultContent && data.ResultContent!=null) {
                 data=data.ResultContent;
                 var observe = data.observe[_areaCode]["1001002"];
                 var forecast = data.forecast["24h"][_areaCode]["1001001"];

                 $$("#index-page .start-content .temperature").text(forecast[0]["004"]+"-"+forecast[0]["003"]+"℃");
                 $$("#index-page .start-content .weather").text(getWeatherText(observe["001"]));
            }
        }, error: function () {

        }
    });

    $$(".check-login").on("click", function (e) {
        //取消原有跳转事件
        e.stopPropagation();
        //保存当前控件
        var mythis = $$(this);
        //跳转判断
        CheckPopLogin(function () {
            window.location.href = mythis.attr("data-href");
        });
    });

    var getWeatherText = function (weather) {
        var wt = "";
        if (weather == "00") {
            wt = "晴";
        } else if (weather == "01") {
            wt = "多云";
        } else if (weather == "02") {
            wt = "阴";
        } else if (weather == "03") {
            wt = "阵雨";
        } else if (weather == "04") {
            wt = "雷阵雨";
        } else if (weather == "05") {
            wt = "雷阵雨伴有冰雹";
        } else if (weather == "06") {
            wt = "雨夹雪";
        } else if (weather == "07") {
            wt = "小雨";
        } else if (weather == "08") {
            wt = "中雨";
        } else if (weather == "09") {
            wt = "大雨";
        } else if (weather == "10") {
            wt = "暴雨";
        } else if (weather == "11") {
            wt = "大暴雨";
        } else if (weather == "12") {
            wt = "特大暴雨";
        } else if (weather == "13") {
            wt = "阵雪";
        } else if (weather == "14") {
            wt = "小雪";
        } else if (weather == "15") {
            wt = "中雪";
        } else if (weather == "16") {
            wt = "大雪";
        } else if (weather == "17") {
            wt = "暴雪";
        } else if (weather == "18") {
            wt = "雾";
        } else if (weather == "19") {
            wt = "冻雨";
        } else if (weather == "20") {
            wt = "沙尘暴";
        } else if (weather == "21") {
            wt = "小到中雨";
        } else if (weather == "22") {
            wt = "中到大雨";
        } else if (weather == "23") {
            wt = "大到暴雨";
        } else if (weather == "24") {
            wt = "暴雨到大暴雨";
        } else if (weather == "25") {
            wt = "大暴雨到特大暴雨";
        } else if (weather == "26") {
            wt = "小到中雪";
        } else if (weather == "27") {
            wt = "中到大雪";
        } else if (weather == "28") {
            wt = "大到暴雪";
        } else if (weather == "29") {
            wt = "浮尘";
        } else if (weather == "30") {
            wt = "扬沙";
        } else if (weather == "31") {
            wt = "强沙尘暴";
        } else if (weather == "53") {
            wt = "霾";
        } else {
            wt = "多云";
        }
        return wt;
    }
}).trigger();
